<?php

/**
 * Description of ContentRegister
 *
 * @author Honza
 */
class ContentRegister implements IMainBodyWithInteraction {

  private $formMessage;
  private $showForm = true;
  private $login;
  private $firstname;
  private $surname;
  private $street;
  private $cp;
  private $city;
  private $postcode;
  private $email;
  private $mobile;

  public function action() {
    
  }

  public function applyForm($formID = NULL) {
    $login = addslashes($_POST['login']);
    $pass1 = addslashes($_POST['heslo1']);
    $pass2 = addslashes($_POST['heslo2']);
    $firstname = addslashes($_POST['jmeno']);
    $surname = addslashes($_POST['prijmeni']);
    $street = addslashes($_POST['ulice']);
    $cp = addslashes($_POST['cp']);
    $city = addslashes($_POST['mesto']);
    $postcode = addslashes($_POST['psc']);
    $email = addslashes($_POST['email']);
    $mobile = addslashes($_POST['mobil']);
    
    //attributes
    $this->login = $login;
    $this->firstname = $firstname;
    $this->surname = $surname;
    $this->street = $street;
    $this->cp = $cp;
    $this->city = $city;
    $this->postcode = $postcode;
    $this->email = $email;
    $this->mobile = $mobile;

    $isError = false;
    $allFilled = false;
    $this->formMessage = "";

    if (!$login || !$pass1 || !$pass2 || !$firstname || !$surname || !$email || !$city) { // všechny položky jsou vyplněny?
      $isError = true;
      $this->formMessage.="You didn´t fill all compulsory fields<br />\n";
    } else {
      $allFilled = true;
    }

    //login
    if ($login && strlen($login) < 4) {
      $isError = true;
      $this->formMessage.="Login is too short - minimum: 4 chars" . BR;
    } elseif (strlen($login) > 10) {
      $isError = true;
      $this->formMessage.="Login is too long - maximum je: 10 chars" . BR;
    } else {
      // check if user login exists
      $result = DB::sql("select * from " . TBL_USERS . " where login='$login'");
      if ($result) {
        $isError = true;
        $this->formMessage.="Existing login, try it again" . BR;
      }
    }
    
    if (!ctype_alnum($login)) {
      $isError = true;
      $this->formMessage.="Login has to contain only alfa-numeric characters" . BR;
    }

    if (strlen($firstname) > 15) {
      $isError = true;
      $this->formMessage.="Name is too long" . BR;
    }
    if (strlen($surname) > 30) {
      $isError = true;
      $this->formMessage.="Surname is too long" . BR;
    }
    
    if ($allFilled) {
      // passwds match
      if ($pass1 == $pass2) {
        
        if (!ctype_alnum($pass1)) {
          $isError = true;
          $this->formMessage.="Password has to contain only alfa-numeric characters" . BR;
        }
      
        if (strlen($pass1) < 4) {
          $isError = true;
          $this->formMessage.="Password is too short" . BR;
        }
      } else {
        $isError = true;
        $this->formMessage.="Passwords don´t match" . BR;
      }
      
      if (strlen($email) > 64) {
        $isError = true;
        $this->formMessage.="Email is too long - maximum: 64 chars" . BR;
      }

      // is email valid?
      if (filter_var("$email", FILTER_VALIDATE_EMAIL)) {
        
      } else {

        $isError = true;
        $this->formMessage.="Email isn´t valid" . BR;
      }
    }

    if (!$isError) {
      // no error, stop showing the form
      $this->showForm = false;

      // encrypt the password for database
      $pass = Auth::encrypt($pass1, $login);



      //id, idaddress, 
      $role = ROLE_USER_ID;
      $auth = Auth::getInstance();
      if ($auth->hasAuthority(ROLE_ADMIN)) {
        var_dump($_POST);
        $role = $_POST['role'];
      }

      $idAdresy = DB::sql("insert into " . TBL_ADDRESSES . " values ( 0, '$street', '$cp', '$city', '$postcode' )", 'id_address');
      DB::sql("insert into " . TBL_USERS . " values ( 0, " . $role . ", $idAdresy, NULL, '$login', '$pass', '$firstname', '$surname', '$email', '$mobile', CURDATE( ), NULL, 'ano')");

      return "The registration was successful, now you can <a href=\"index.php?where=login\">log in</a>" . BR;
    }
  }

  public function getContent() {
    $content = '';
    $content .= '<h2>Register</h2>';

    $formID = $this->getSubmitedForm();
    if ($formID > 0) {
      $content .= $this->applyForm($formID);
    }

    if ($this->showForm) {
      if (!empty($this->formMessage)) {
        $content .= '<p class="error">' . $this->formMessage . '</p>';
      }
      $content .= $this->getForm();
    }

    return $content;
  }

  public function getForm($formID = NULL, $error = NULL) {
    $f = new Form();
    $f->startForm("self", "post", null, array("class" => "form_settings"));
    $f->addDescription("Login information");
    $f->addLabel("Login*", "login")->addInput("text", "login", $this->login);
    $f->addLabel("Password*", "heslo1")->addInput("password", "heslo1");
    $f->addLabel("Password (again)*", "heslo2")->addInput("password", "heslo2");
    $f->addDescription("Personal information");
    $f->addLabel("Name*", "jmeno")->addInput("text", "jmeno", $this->firstname);
    $f->addLabel("Surname*", "prijmeni")->addInput("text", "prijmeni", $this->surname);
    $f->addLabel("Email*", "email")->addInput("text", "email", $this->email);
    $f->addLabel("Mobile", "mobil")->addInput("text", "mobil", $this->mobile);
    $f->addDescription("Address");
    $f->addLabel("Street", "ulice")->addInput("text", "ulice", $this->street);
    $f->addLabel("Street number", "cp")->addInput("text", "cp", $this->cp);
    $f->addLabel("City*", "mesto")->addInput("text", "mesto", $this->city);
    $f->addLabel("Postal code", "psc")->addInput("text", "psc", $this->postcode);


    $auth = Auth::getInstance();
    if ($auth->hasAuthority(ROLE_ADMIN)) {
      $f->addDescription("Role");
      $sql_role = 'select * from ' . TBL_ROLES . ' order by title';
      $f->addLabel('Role', 'role')->addSelectionBox('role', DB::sql($sql_role), 'title', 'id_role');
    }
    $f->addLabel("&nbsp;", "")->addInput("submit", "registerme", "Register", array("class" => "submit"));
    return $f->endForm()->getForm();
  }

  public function getSubmitedForm() {
    if (isset($_POST['registerme'])) {
      return 1;
    }

    return -1;
  }

  public function getTable($tableID = NULL) {
    
  }

}

?>
